<template>
  <div>
    <el-dialog :title="dialogDictItemIndex.title" :visible.sync="dialogDictItemIndex.show" width="55%" destroy-on-close @close='closeDialog' @open='openDialog'
               :close-on-click-modal="false" :close-on-press-escape="false" :modal-append-to-body="false">
    <div class="head-container">
      <div>
        <span>
          <span class="filter-item">
            <el-button type="primary" icon="el-icon-plus" @click="showAddDialog">新增</el-button>
          </span>
        </span>
      </div>
    </div>
    <el-card style="margin-top: 10px">
    <el-table ref="table" border :data="tableData" style="width: 100%" row-key="id" lazy>>
        <el-table-column type="index" label="序号" width="55"> </el-table-column>
        <el-table-column prop="type" label="类型"> </el-table-column>
        <el-table-column prop="value" label="数据值" align="center"></el-table-column>
        <el-table-column prop="label" label="标签名" align="center"></el-table-column>
        <el-table-column prop="description" label="描述" align="center"></el-table-column>
        <el-table-column prop="sort" label="排序" align="center"></el-table-column>
        <el-table-column prop="remarks" label="备注信息" align="center"></el-table-column>
        <el-table-column label="操作" width="250" align="center">
        <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="showEditDialog(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
        </el-table-column>
    </el-table>
    </el-card>
    <!-- 分页 -->
    <pagination :total="total" :page.sync="listQuery.current" :limit.sync="listQuery.size" @pagination="getList"/>
    <DialogDictItem :dialogDictItem="dialogDictItem" :formData="formData" @handleSubmit="handleSubmit" @handleSubmitEdit="handleSubmitEdit"></DialogDictItem>
    </el-dialog>
  </div>
</template>

<script>

import { dictItemPageList, deleteDictItem, createDictItem, updateDictItem } from "@/api/system/dict";
import Pagination from "@/components/Pagination"; // el分页
import DialogDictItem from "@/components/System/dict/dialogDictItem.vue";

export default {
  props: ['dialogDictItemIndex', 'dictIdData'],
  data() {
    return {
      //表格数据
      tableData: [],
      //条件查询搜索
      total: 0,
      formData: {},
      listQuery: {
        current: 1,
        size: 10,
        dictId: undefined,
      },
      dialogDictItem: {
        show: false,
      },
    }
  },
  methods: {
    // 弹窗打开触发的方法
    openDialog() {
        this.listQuery.dictId = this.dictIdData.dictId;
        this.getList();
    },
    // 弹窗关闭触发的方法
    closeDialog() {

    },
    getList() {
        dictItemPageList(this.listQuery).then((res) => {
            this.tableData = res.data.records;
            this.total = res.data.total;
            this.listQuery.current = res.data.current;
            this.listQuery.size = res.data.size;
            setTimeout(() => {
                this.listLoading = false;
            }, 1.5 * 1000);
        });
    },
    //删除
    handleDelete(row) {
      this.$confirm('此操作将永久删除数据为"' + row.label + '"', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDictItem(row.id).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getList();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //新增按钮触发
    showAddDialog() {
      this.dialogDictItem = {
        show: true,
        title: "新增字典项",
        option: "add",
      };
      this.formData = {
        dictId: this.dictIdData.dictId,
        type: this.dictIdData.type,
      };
    },
    //编辑按钮出发弹框
    showEditDialog(row) {
      this.dialogDictItem = {
        show: true,
        title: "修改字典项",
        option: "edit",
      };
      this.formData = {
        id: row.id,
        dictId: row.dictId,
        type: row.type,
        value: row.value,
        label: row.label,
        description: row.description,
        sort: row.sort,
        remarks: row.remarks,
      };
    },
    //新增提交
    handleSubmit() {
      createDictItem(this.formData).then((res) => {
        if (res.data == true) {
          this.dialogDictItem.show = false;
          this.$message.success("新增成功！");
          this.getList();
        } else {
          this.dialogDictItem.show = false;
          this.$message.success(res.message);
        }
      });
    },
    //修改提交
    handleSubmitEdit() {
      updateDictItem(this.formData).then((res) => {
        if (res.data == true) {
          this.dialogDictItem.show = false;
          this.$message.success("修改成功！");
          this.getList();
        } else {
          this.dialogDictItem.show = false;
          this.$message.success(res.message);
        }
      });
    },
  },
  components: {
    Pagination,
    DialogDictItem,
  },
}
</script>

<style lang="sass" scoped>
</style>